iT邦幫忙

2024 iThome 鐵人賽

DAY 25
0

時鐘

用html和css先做出時鐘的畫面,但此時時針、分針、秒針都還不會動
https://ithelp.ithome.com.tw/upload/images/20241004/20168638owxTPU9BFR.png
HTML

    <div class="top">
        <span>鐵人賽D5 時鐘</span>
    </div>
    <div class="clock">
      <div class="clock-face">
        <div class="hand hour-hand"></div>
        <div class="hand min-hand"></div>
        <div class="hand second-hand"></div>
        </div>
    </div>

CSS

  <style>
    html {
      background:#c011c0 url(http://unsplash.it/1500/1000?image=881&blur=50);
      background-size:cover;
      font-family:'helvetica neue';
      text-align: center;
      font-size: 10px;
    }

    body {
      margin: 0;
      font-size: 2rem;
      display:flex;
      flex:1;
      min-height: 100vh;
      align-items: center;
    }
    .top{
    border-bottom:1px solid
        rgb(255, 255, 255);
    box-sizing: border-box;
    height: 35px;
    display: flex;
    background-color: transparent;
    color: #fff;
    padding: 10px;
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 3;
    transition: background-color 0.15s;
}

    .clock {
      width: 70rem;
      height: 70rem;
      border:20px solid white;
      border-radius:50%;
      margin:50px auto;
      position: relative;
      padding:2rem;
      box-shadow:
        0 0 0 4px rgba(0,0,0,0.1),
        inset 0 0 0 3px #EFEFEF,
        inset 0 0 10px black,
        0 0 10px rgba(0,0,0,0.2);
    }

    .clock-face {
      position: relative;
      width: 100%;
      height: 100%;
      transform: translateY(-3px); /* account for the height of the clock hands */
    }

    .hand {
      width:50%;
      height:6px;
      background:black;
      position: absolute;
      top:50%;
      transform-origin: 100%;
      transform: rotate(90deg);
      transition: all 0.05s;
      transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
    }
</style>

接著要用Javascript實現他的動作:
首先先選擇代表秒針、分針和時針的HTML元素:

  const secondHand = document.querySelector('.second-hand');
  const minsHand = document.querySelector('.min-hand');
  const hourHand = document.querySelector('.hour-hand');

這三行程式分別代表了三個指針的元素,並將它們存取在變量中
接著定義setDate函數,以更新指針的位置,並用Date取得當前時間

  function setDate() {
    const now = new Date();

計算秒針的旋轉幅度

const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;

計算時針,分針的旋轉幅度

const mins = now.getMinutes();
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
minsHand.style.transform = `rotate(${minsDegrees}deg)`;

const hour = now.getHours();
const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
hourHand.style.transform = `rotate(${hourDegrees}deg)`;

最後設置每秒調用一次setDate函數&初始化setDate

setInterval(setDate, 1000);
setDate();

以下是完成圖:

https://ithelp.ithome.com.tw/upload/images/20241004/20168638gs36lpwHiE.png


上一篇
# JavaScript實作DAY1
下一篇
# JavaScript實作DAY3
系列文
解鎖第一個人生成就清單:Javascript鐵人學習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言